package com.logic.landseaserver.models.payment.ws;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.common.util.StringTools;
import com.logic.landseaserver.models.payment.bean.PaymentQueryRequest;
import com.logic.landseaserver.models.payment.bean.PaymentRequest;
import com.logic.landseaserver.models.payment.service.PaymentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：PaymentRest</br>
 * 类描述：通用支付</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月17日 下午2:35:52</br>
 * 
 * @version 1.0
 *
 */
@Path("/payment")
@Api(value = "支付")
public class PaymentRest
{
    protected static final Logger LOGGER = LoggerFactory.getLogger(PaymentRest.class);
    
    /** 支付入口组件 **/
    @Autowired
    
    private PaymentService paymentService;
    /**
     * 
     * [简要描述]：所有支付入口</br>
     * [详细描述]：唯一入口</br>
     * [作者]：Aaron(2017-07-17)</br>
     *
     * @param paymentRequest
     * @return
     *
     */
    @POST
    @Path("/paymentRequest")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "支付")
    public Response pay(@ApiParam(value = "paymentRequest", required = true) PaymentRequest paymentRequest)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Map<String, Object> resultMap = paymentService.pay(paymentRequest);
            if (MapUtils.isNotEmpty(resultMap))
            {
                LOGGER.info("paymentRequest|Return Data|" + resultMap.toString());
                map.setData(resultMap);
            }
            
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "paymentRequest|pay ", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    /**
     * 
     * [简要描述]：支付回调-POS</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-19)</br>
     *
     * @param paymentRequest
     * @return
     *
     */
    @POST
    @Path("/paymentResponse/pos")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "POS支付回调")
    public Response posCallBack(
        @ApiParam(value = "pos paymentResponse", required = true) Map<String,Object> paymentResponseMap)
    {
        LOGGER.info("paymentResponse|POS支付结果通知|" + paymentResponseMap);
        
        CommonResultMap map = new CommonResultMap();
        try
        {
            String paymentIdStr = StringTools.getString(paymentResponseMap.get("paymentId"));
            // 对paymentId解密
            Map<String, Object> resultMap = paymentService.payCallback(paymentIdStr, new HashMap<String,Object>());
            map.setData(resultMap);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "paymentResponse|POS支付通知处理失败 ", e, map);
        }
        
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    /**
     * 
     * [简要描述]：支付查询</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-17)</br>
     *
     * @param paymentRequest
     * @return
     *
     */
    @POST
    @Path("/paymenQuery")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "支付查询")
    public Response paymenQuery(@ApiParam(value = "paymenQuery", required = true) PaymentQueryRequest paymentQuery)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Map<String, Object> resultMap = paymentService.paymentQuery(paymentQuery);
            if (MapUtils.isNotEmpty(resultMap))
            {
                LOGGER.info("paymentRequest|Return Data|" + resultMap.toString());
                map.setData(resultMap);
            }
            
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "paymentRequest|pay ", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
